/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.hotTopicLike.service.impl;

import co.yixiang.api.ApiResult;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.hotTopic.domain.YxHotTopic;
import co.yixiang.modules.hotTopic.service.YxHotTopicService;
import co.yixiang.modules.hotTopicLike.domain.YxLike;
import co.yixiang.modules.hotTopicLike.service.YxLikeService;
import co.yixiang.modules.hotTopicLike.service.dto.YxLikeDto;
import co.yixiang.modules.hotTopicLike.service.dto.YxLikeQueryCriteria;
import co.yixiang.modules.hotTopicLike.service.mapper.YxLikeMapper;
import co.yixiang.modules.pay.zhifubao.EmptyUtils;
import co.yixiang.utils.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
* @author hupeng
* @date 2021-08-12
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "yxLike")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxLikeServiceImpl extends BaseServiceImpl<YxLikeMapper, YxLike> implements YxLikeService {

    private final IGenerator generator;
    private final YxHotTopicService yxHotTopicService;

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(YxLikeQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxLike> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YxLikeDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<YxLike> queryAll(YxLikeQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YxLike.class, criteria));
    }


    @Override
    public void download(List<YxLikeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxLikeDto yxLike : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("用户id", yxLike.getUid());
            map.put("热门话题id", yxLike.getHotTopicId());
            map.put("创建时间", yxLike.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }



    @Override
    public ApiResult saveOrDeleteHotTopicLike(Integer hotTopicId, Long uid) {
        YxHotTopic yxHotTopic = yxHotTopicService.getById(hotTopicId);
        if(EmptyUtils.isEmpty(yxHotTopic)){
            return ApiResult.fail("未找到该热门话题");
        }
        YxLike yxLike = baseMapper.selectOne(new LambdaQueryWrapper<YxLike>().eq(YxLike::getHotTopicId,hotTopicId).eq(YxLike::getUid,uid));
        if(EmptyUtils.isNotEmpty(yxLike)){
            baseMapper.deleteById(yxLike);
            yxHotTopic.setLikeNumber(EmptyUtils.isNotEmpty(yxHotTopic.getLikeNumber())?(yxHotTopic.getLikeNumber()-1)<0?0:yxHotTopic.getLikeNumber()-1:0);
            yxHotTopicService.updateById(yxHotTopic);
            return ApiResult.ok("点赞成功");
        }else{
            yxLike = new YxLike();
            yxLike.setHotTopicId(hotTopicId);
            yxLike.setUid(uid);
            yxLike.setCreateTime(new Date());
            baseMapper.insert(yxLike);
            yxHotTopic.setLikeNumber(EmptyUtils.isNotEmpty(yxHotTopic.getLikeNumber())?yxHotTopic.getLikeNumber()+1:1);
            yxHotTopicService.updateById(yxHotTopic);
            return ApiResult.ok("取消点赞成功");
        }
    }

}
